clear
close all
clc

fntsize=14;
w=500;
h=w*2/(1+sqrt(5));
col=[0;0.5;0.8;1]*ones(1,3);
corr=0.003;
se=1.96;
printyes=1;

%% Load data
[data,labels]=xlsread('DataForFiguresTables.xlsx','Polynomial','A3:Q24');
[~,N]=size(labels);
for i=1:N
    assignin('base',labels{1,i},data(:,i));
end

%% Plot Fig. VI
figure('position',[0 0 w h]);
b=plot([min(ls) max(ls)],[0 0]);
hold on
a = plot(ls,ln_va_l_tilde7a,ls,ln_va_l_tilde7a+ln_va_l_tilde7a_se*se,ls,ln_va_l_tilde7a-ln_va_l_tilde7a_se*se,...
         ls,ln_w_tilde7a,ls,ln_w_tilde7a+ln_w_tilde7a_se*se,ls,ln_w_tilde7a-ln_w_tilde7a_se*se);
grid
set(a(1)  ,'linestyle','-' ,'color',col(1,:),'linewidth',2)
set(a(2:3),'linestyle','--','color',col(1,:),'linewidth',2)
set(a(4)  ,'linestyle','-' ,'color',0.8*col(3,:),'linewidth',2)
set(a(5:6),'linestyle','--','color',0.8*col(3,:),'linewidth',2)
set(b,'linestyle','-','color','k','linewidth',1)
set(gca,'ticklabelinterpreter','latex','xlim',[min(ls) max(ls)],'xtick',(min(ls):0.1:max(ls)),'xticklabel',{'$0$';'$0.1$';'$0.2$';'$0.3$';'$0.4$';'$0.5$';'$0.6$';'$0.7$';'$0.8$';'$0.9$';'$1.0$'},'ylim',[-0.75 2.25],'ytick',(-0.75:0.75:2.25),'fontsize',fntsize)
xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize)
lgnd=legend([a(1);a(4)],{'Rel. value added/worker $\widetilde{py/l}$';'Rel. wage $\widetilde{w}$'},'location','ne','interpreter','latex','fontsize',fntsize);
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_Components_WandVA_L.eps','-depsc')
end

%% Plot Fig. VIII
figure('position',[0 0 w h]);
a = bar(ls,[ln_p6a ln_tvs_l_tilde6a-ln_p6a],1,'stacked');
grid
a(1).FaceColor = col(2,:);
a(2).FaceColor = col(3,:);
set(gca,'ticklabelinterpreter','latex','xlim',[min(ls)-0.025 max(ls)+0.025],'xtick',(min(ls):0.1:max(ls)),'ylim',[-0.4 0.8],'ytick',(-0.4:0.2:0.8),'fontsize',fntsize)
xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize)
% ylabel('Rel. prices and rel. phys. productivity','interpreter','latex','fontsize',fntsize)
title('Components of rel. sales/worker $\widetilde{pq/l}$','interpreter','latex','fontsize',fntsize);
lgnd=legend(a,{'Rel. prices $\widetilde{p}$';'Rel. physical productivity $\widetilde{q/l}$'},'interpreter','latex','location','ne','fontsize',fntsize);
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_Components_TVS_LandP_matched.eps','-depsc')
end

%% Plot Fig. XIV
figure('position',[0 0 w h]);
b=plot([min(ls) max(ls)],[0 0]);
hold on
a = plot(ls,ln_ca_te,ls,ln_ca_te+ln_ca_te_se*se,ls,ln_ca_te-ln_ca_te_se*se);
grid
set(a(1)  ,'linestyle','-' ,'color',col(1,:),'linewidth',2)
set(a(2:3),'linestyle','--','color',col(1,:),'linewidth',2)
set(b,'linestyle','-','color','k','linewidth',1)
set(gca,'ticklabelinterpreter','latex','xlim',[min(ls) max(ls)],'xtick',(min(ls):0.1:max(ls)),'xticklabel',{'$0$';'$0.1$';'$0.2$';'$0.3$';'$0.4$';'$0.5$';'$0.6$';'$0.7$';'$0.8$';'$0.9$';'$1.0$'},'ylim',[-0.4 0.8],'ytick',(-0.4:0.2:0.8),'fontsize',fntsize)
xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize)
lgnd=legend([a(1)],{'Advertising exp./worker $\widetilde{ca/l}$'},'location','ne','interpreter','latex','fontsize',fntsize);
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_Adv.eps','-depsc')
end

%% Plot Fig. B.3
figure('position',[0 0 w h]);
hold on
a = plot(ls,[ln_tvs_l_tilde7a ln_tvs_l_tilde7a+ln_tvs_l_tilde7a_se*se ln_tvs_l_tilde7a-ln_tvs_l_tilde7a_se*se],ls,[ln_tvs_l_tilde6a ln_tvs_l_tilde6a+ln_tvs_l_tilde6a_se*se ln_tvs_l_tilde6a-ln_tvs_l_tilde6a_se*se]);
b=plot([min(ls) max(ls)],[0 0]);
grid
set(a(1)  ,'linestyle','-' ,'color',col(1,:),'linewidth',2)
set(a(2:3),'linestyle','--','color',col(1,:),'linewidth',2)
set(a(4)  ,'linestyle','-' ,'color',0.8*col(3,:),'linewidth',2)
set(a(5:6),'linestyle','--','color',0.8*col(3,:),'linewidth',2)
set(b,'linestyle','-','color','k','linewidth',1)
set(gca,'ticklabelinterpreter','latex','xlim',[min(ls) max(ls)],'xtick',(min(ls):0.1:max(ls)),'ylim',[-0.5 1.7],'ytick',(-0.5:0.5:1.5),'fontsize',fntsize)
xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize)
% ylabel('Rel. sales/worker $\widetilde{pq/l}$','interpreter','latex','fontsize',fntsize)
title('Rel. sales/worker $\widetilde{pq/l}$','interpreter','latex','fontsize',fntsize);
lgnd=legend([a(1) a(4)],{'Full Census Sample';'Matched Price Sample'},'interpreter','latex','location','ne','fontsize',fntsize);
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_Components_TVS_L2.eps','-depsc')
end